<?php

/**
 * @copyright (C) 2008 by Slashes & Dots Sdn Bhd - All rights reserved!
 * @license http://www.azrul.com Copyrighted Commercial Software
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.plugin.plugin' );

// require jom-social apps
require_once( JPATH_BASE .DS.'components' .DS.'com_community' .DS.'libraries' .DS.'core.php');

class plgActStreamMTree extends JPlugin
{
	/**
	 * Example after display content method
	 *
	 * Method is called by the view and the results are imploded and displayed in a placeholder
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 * @return	string
	 */
	function onAfterDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;
		
		// No need in admin panel
		if( $mainframe->isAdmin() ) return;
		
		$option = JRequest::getCmd('option', '');
		$task   = JRequest::getCmd('task', '');
		$user_id 	= JRequest::getVar('id', 0);		
		
		/*User is not logged in, you can't assign points or add activity*/
		if($user_id < 0)
			return;
			
		if ($option != 'com_mtree' )
			return;
		
		switch($task)
		{
			/*visit a image*/
			case "viewimage":
				break;

			/* visit a gallery */
			case "viewgallery":
				break;
			
			/*visit a list */
			case "viewlink":
				break;

			/* Visit a URL */
			case "visit":
				break;

			/* Reviews */
			case "addreview":
				break;

			/* Ratings */
			case "addrating":
				break;
	
			/* Favourite */
			case "fav":
				break;

			/* Vote review */
			case 'votereview':
				break;

			/* Report review */
			case "send_reportreview":
				break;

			/* Reply review */
			case 'send_replyreview':
				break;

			/* Recommend to Friend */
			case "send_recommend":
				break;

			/* Contact Owner */
			case "send_contact":
				break;

			/* Report Listing */
			case "send_report":
				break;

			/* Claim Listing */
			case "send_claim":
				break;

			/* Add Listing */
			case "savelisting":
				break;

			/* Add Category */
			case "addcategory2":
				break;

			/* Delete Listing */
			case "confirmdelete":
				break;
			default :
				return;
		}
	}


	
	function onProfileDisplay()
	{
		$config	=& CFactory::getConfig();
		$myJconfig =& JFactory::getConfig();
		
		$now = date( "Y-m-d H:i:s", time()+ $myJconfig->getValue('config.offset') *60*60 );
				
		/*
		$avatarModel		=& CFactory::getModel( 'avatar' );
		*/
		$user		= CFactory::getActiveProfile();
		$userName	= $user->getDisplayName();

				
		$db =& JFactory::getDBO();
		$db->setQuery("SELECT COUNT(*) FROM #__mt_links WHERE "
			. "\n	link_published='1' AND link_approved='1' AND user_id ='".$user->id."'"
			. "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now'  ) "
			. "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down >= '$now' ) "
			);
		$total_links = $db->loadResult();
		
	
		ob_start();
 		if( $total_links ){

			// Get latest listings
	        $maxCount = $this->params->get('count', 5);
			$db->setQuery( "SELECT l.*  FROM (#__mt_links AS l, #__mt_cl AS cl)"
			. "\n WHERE link_published='1' AND link_approved='1' AND user_id='".$user->id."' "
			. "\n AND l.link_id = cl.link_id AND cl.main = '1'"
			. "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now'  ) "
			. "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down >= '$now' ) "
			. "\n ORDER BY l.link_created DESC "
			."\n LIMIT 0, " . $maxCount);	

			$items = $db->loadObjectList();
		}else{
			$userId = "";
			$userName = ""; 
			$items = "";
		}
		
		$myItemid = '&amp;Itemid='.$this->getItemid();
		
    	$cache =& JFactory::getCache('plgCommunityMTree');
    	$cache->setCaching($this->params->get('cache', 1));
		$callback = array('plgCommunityMTree', '_getMTreeHTML');		
		$content = $cache->call($callback, $user->id, $userName, $items, $myItemid);
		return $content; 		
	}
	
	function _getMTreeHTML($userId, $userName, $items, $myItemid){
		ob_start();
 		
		if( !empty($items) ) {
				?>
				<div>
				<table width="100%">
				
				<?php
				foreach ($items as $item ){
						$myName		= $item->link_name;
						$myURL 		= JRoute::_("index.php?option=com_mtree&task=viewlink&link_id=".$item->link_id."&amp;Itemid=".$myItemid);               		 
            $myDate	 = new JDate($item->link_created);
						$myHits		= $item->link_hits; ?>
			            
					<ul class="list"><li>
					<tr>
						<td width="70%"><a href="<?php echo $myURL;?>"> <?php echo stripslashes ($myName); ?> </a></td>
						<td width="20%" align="left"><?php echo $myDate->toFormat(JText::_('DATE_FORMAT_LC3'));?></td>
						<td width="10%" align="right"><?php echo $myHits ?> hits</td>
					</tr>
					</li></ul><?php
				}?>
				</table>
				<div style="clear: both;"></div>
				</div>
				<?php
		} else {
				?>
		        <div class="content-nopost">
		            <?php echo $userName . ' ' . JText::_('hasn\'t contributed any listing yet'); ?>
		        </div>
				<?php
 		}
 		
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
	
	function onAppDisplay()
	{
		ob_start();
		$limit=0;
		$html= $this->onProfileDisplay($limit);
		echo $html;
		
		$content	= ob_get_contents();
		ob_end_clean(); 
	
		return $content;
		
	}
	
	/**
	 * Return itemid for mTree
	 */	 	
	function getItemid(){
		$db =& JFactory::getDBO();

       	$db->setQuery("SELECT id FROM #__menu WHERE link = 'index.php?option=com_mtree' AND published = 1");
       	$Itemid = $db->loadResult();
	
	    if ($Itemid < 1) {
			$Itemid = 0;
	    }
	    
	    return $Itemid;
	}
}